/**
 * @Title: HomeAdapter.java
 * @Package: com.jason.fingerprint.adapter
 * @Descripton: TODO
 * @Author: Jason.Zhang zhangyujn1989ok@gmail.com
 * @Date: 2014年10月19日 上午9:21:22
 * @Version: V1.0
 */
package com.jason.fingerprint.adapter;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.jason.fingerprint.R;
import com.jason.fingerprint.beans.ui.AppBean;
import com.jason.fingerprint.widget.HomeModelItem;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: LauncherAdapter
 * @Description: LauncherAdapter GridView 应用启动适配器
 * @Author: Jason.Zhang zhangyujn1989ok@gmail.com
 * @Date: 2014年10月19日 上午9:21:22
 */
public class LauncherAdapter extends BaseAdapter {

    private static final String TAG = LauncherAdapter.class.getSimpleName();

    private Context mContext;
    private LayoutInflater mLayoutInflater;
    private List<AppBean> mData = new ArrayList<AppBean>();

    public LauncherAdapter() {
        this.mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mData = new ArrayList<AppBean>();
    }

    public LauncherAdapter(Context context, List<AppBean> beans) {
        this.mContext = context;
        this.mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.mData = beans;
    }

    @Override
    public int getCount() {
        return isEmptyOfModelBeans() ? mData.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return isEmptyOfModelBeans() ? mData.get(position) : null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Holder holder = null;
        if (convertView == null) {
            convertView = mLayoutInflater.inflate(R.layout.itme_home_gridview, null);
            holder = new Holder();
            holder.mMainView = (HomeModelItem) convertView.findViewById(R.id.main);
            holder.mTextView = (TextView) convertView.findViewById(R.id.item_home_gridview_title);
            holder.mImageView = (ImageView) convertView.findViewById(R.id.item_home_gridview_icon);
            convertView.setTag(holder);
        } else {
            holder = (Holder) convertView.getTag();
        }
        AppBean bean = (AppBean) getItem(position);
        if (bean != null) {
//			Drawable drawable= mContext.getResources().getDrawable(bean.getIconId());
//			/// 这一步必须要做,否则不会显示.
//			drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
//			holder.mTextView.setCompoundDrawables(null,drawable,null,null);
            holder.mTextView.setText(bean.name);
            holder.mTextView.setTextColor(mContext.getResources().getColor(R.color.white));
            holder.mImageView.setBackground(bean.icon);
        }
        holder.mMainView.setBackgroundColor(mContext.getResources().getColor(R.color.transparent));
        return convertView;
    }

    class Holder {
        HomeModelItem mMainView;
        TextView mTextView;
        ImageView mImageView;
    }

    //判断当前数据是否为空
    public boolean isEmptyOfModelBeans() {
        return mData != null && mData.size() > 0;
    }

}
